<script lang="ts" setup>
import { Search } from '@element-plus/icons-vue'
import { useHomeStore } from '@/stores/home'
import { storeToRefs } from 'pinia'
import { ref, computed } from 'vue'
const searchKeyword = ref('')

const store = useHomeStore()
const { filterTree } = storeToRefs(store)
// 过滤后的树数据
const filteredTree = computed(() => {
  if (!searchKeyword.value.trim()) return {};
  return filterTreeFun(filterTree.value, searchKeyword.value.trim());
});
console.log("🚀 ~ filteredTree ~ filteredTree:", filteredTree)

// 不用递归过滤函数
const filterTreeFun = (nodes, keyword: string) => {
  const nnodes = nodes
    .map(node => ({ ...node })) // 浅拷贝避免修改原数据
    .filter(node => {
      console.log("🚀 ~ filterTreeFun ~ node:", node)

      const lowerKeyword = keyword.toLowerCase();
      const isMatch = node.name.toLowerCase().includes(lowerKeyword);
      return isMatch
    });
  console.log("🚀 ~ filterTreeFun ~ nnodes:", nnodes)
  return nnodes
};
const goBack = () => {
  searchKeyword.value = ""
  store.setSearchFlag(false)
}

</script>
<template>
  <div class="flex ">
    <div class="close-icon" @click="goBack"></div>
    <el-input v-model="searchKeyword" style="width: 310px" placeholder="" :prefix-icon="Search" clearable autofocus
      class="custom-input" />
  </div>
  <div class="filter-content-item" :id="`filterContentItem${index}`" v-for="(item, index) in filteredTree"
    :key="item.id">
    <div class="content-body">
      <div class="content-item" v-for="child in item.children" :key="child.id">
        <div class="item-icon-container">
          <div class="icon-pic" :style="{
            backgroundImage: `url(${child.icon})`,
          }"></div>
          <div class="icon-count">{{ child.children.length }}</div>
          <div class="selected-icon" v-if="child.active"></div>
        </div>
        <div class="content-item-name">{{ child.name }}</div>
      </div>
    </div>
  </div>
</template>
<style lang="less" scoped>
.flex {
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 20px;
}

.close-icon {
  width: 30px;
  height: 30px;
  background-image: url('../assets/images/ui/close-icon.png');
  background-size: cover;
  cursor: pointer;
}


.filter-content-item {
  border-bottom: 1px solid rgba(74, 83, 102, 0.5);

  .content-head {
    padding: 16px 0 0 2px;

    .head-title {
      color: #d3bc8e;
      font-size: 14px;
    }
  }

  .content-body {
    margin: 22px;
    display: flex;
    flex-wrap: wrap;

    .content-item {
      position: relative;
      cursor: pointer;
      margin-bottom: 15px;
      margin-right: 10px;

      &.active {
        .item-icon-container {
          &::after {
            position: absolute;
            display: block;
            content: '';
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border: 0.5px solid #d3bc8e;
            border-radius: 6px;
            box-sizing: border-box;
            z-index: 2;
          }
        }

        .content-item-name {
          color: #d3bc8e;
        }
      }

      .item-icon-container {
        position: relative;
        width: 56px;
        height: 56px;
        border-radius: 6px;
        background-color: #323947;

        .icon-pic {
          width: 100%;
          height: 100%;
          background-size: cover;
        }

        .selected-icon {
          position: absolute;
          top: 0;
          right: -1px;
          width: 24px;
          height: 14px;
          background-size: cover;
          background-image: url('../assets/images/ui/select-icon.png');
        }

        .icon-count {
          position: absolute;
          font-size: 10px;
          right: 0;
          bottom: 0;
          line-height: 13px;
          color: #9b9c9f;
          background-color: #323947;
          padding: 0 4px;
          border-radius: 6px 0 6px 0;
        }
      }

      .content-item-name {
        margin-top: 5px;
        font-size: 12px;
        color: hsla(39, 34%, 89%, 0.75);
        max-width: 57px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        text-align: center;
      }
    }
  }
}
</style>
